﻿using InhollandAir.App_Code;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;

namespace InhollandAir
{
    public class Global : System.Web.HttpApplication
    {

        protected void Application_Start(object sender, EventArgs e)
        {
            if (Application["ActiveUsers"] == null)
            {
                Application["ActiveUsers"] = new Dictionary<string, User>();
            }
        }

        protected void Session_Start(object sender, EventArgs e)
        {
        }

        protected void Application_BeginRequest(object sender, EventArgs e)
        {

        }

        protected void Application_AuthenticateRequest(object sender, EventArgs e)
        {

        }

        protected void Application_Error(object sender, EventArgs e)
        {
            // Code that runs when an unhandled error occurs

            Exception objErr = Server.GetLastError().GetBaseException();

            string err = "Error in: " + Request.Url.ToString() +

                              ". Error Message:" + objErr.Message.ToString();

            // Log the error

            Logging.WriteError(err); 
        }

        protected void Session_End(object sender, EventArgs e)
        {
            //remove user data in application cache if users is logged on
            Dictionary<string, User> ActiveUserCache = (Dictionary<string, User>)Application["ActiveUsers"];
            if (ActiveUserCache.ContainsKey(Session.SessionID))
            {
                ActiveUserCache.Remove(Session.SessionID);
                Application["ActiveUsers"] = ActiveUserCache;
            }
        }

        protected void Application_End(object sender, EventArgs e)
        {
            Application["ActiveUsers"] = null;
        }
    }
}